package server;

import java.util.LinkedList;

public class OutboundMessages {
	private LinkedList<Message> messages = new LinkedList<Message>();

	/**
	 * Adds message to the outbound queue.
	 * Also writes message to server logfile.
	 * @param msg Message to send.
	 */
	public synchronized void addMessage(Message msg) {
		messages.add(msg);
		try {
			Server.log.println(msg.toString(), true);			
		} catch (NullPointerException e) {
			Server.log.println("null message", true);
		}
		this.notifyAll(); 									// Broadcaster.notify()
	}
	/**
	 * Gets message from outbound queue.
	 * @return Message
	 */
	public synchronized Message getMessage() { 
		try {
			while (messages.isEmpty())
				this.wait(); 
		} catch (InterruptedException e) {}

		Message msg = messages.getFirst();
		messages.removeFirst();
		return msg;
	}
}
